using System;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for SQLServerClass
/// </summary>
public partial class SQLServerClass
{    
    
	public string[] LoginAuthentication(
	string email, string password)
	{
	    errorMessage="";
        string[] valueToGet = new string[3]{"","",""};
	    try
	    {
	        OpenConnection();
	        sqlCommand = new SqlCommand();
	        sqlCommand.CommandText="SPLogin";
	        sqlCommand.Connection=sqlConnection;
	        sqlCommand.CommandType= 
	        CommandType.StoredProcedure;
            sqlCommand.Parameters.AddWithValue(
            "@Type", "0");
	        sqlCommand.Parameters.AddWithValue(
	        "@Email",email);
	        sqlDataReader = 
	        sqlCommand.ExecuteReader();
	        if(sqlDataReader.Read())
	        {
	            Common cls = new Common();
	            if(Convert.ToString(
                sqlDataReader["Password"]) == 
                cls.RCVEncrypting(password,"rcv"))
	            {
	                valueToGet[0] = "OK";
	                valueToGet[1]=Convert.ToString(
	                sqlDataReader["UserID"]);
                    valueToGet[2] = Convert.ToString(
                    sqlDataReader["FullName"]); 
                }
                else
                    valueToGet[0] = "P";
	        }
	        else
	        {
                valueToGet[0] = "U";
	        }
	        sqlDataReader.Close();
	        sqlDataReader.Dispose();
             
            CloseConnection();          
	    }
	    catch(Exception ex)
	    {
	        errorMessage=ex.Message;
	    }
	    return valueToGet;
	}
	
    public bool ChangePassword(
    string email, string OldPassword, string newPassword)
    {
        errorMessage = "";
        bool changePasssord=true;
        try
        {
            OpenConnection();
            sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "SPChangePassword";
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandType =
            CommandType.StoredProcedure;
            Common cls = new Common();
            sqlCommand.Parameters.AddWithValue(
            "@Type", "0");
            sqlCommand.Parameters.AddWithValue(
            "@Email", email);
            sqlCommand.Parameters.AddWithValue(
            "@OldPassword", cls.RCVEncrypting(OldPassword, "rcv"));
            sqlCommand.Parameters.AddWithValue(
            "@NewPasssord", cls.RCVEncrypting(newPassword, "rcv"));
            SqlParameter sqlParameter = new SqlParameter("ChangePassword",null);
            sqlParameter.Direction = ParameterDirection.ReturnValue;
            sqlCommand.ExecuteScalar();
            changePasssord = (Convert.ToString(
            sqlParameter.Value) == "OK");
            sqlDataReader.Close();
            sqlDataReader.Dispose();

            CloseConnection();
        }
        catch (Exception ex)
        {
            errorMessage = ex.Message;
        }
        return changePasssord;
    }
    public string ForgotPassword(
    string email, string fullName, string dateOfBirth)
    {
        errorMessage = "";
        string forgotPasssord = "";
        try
        {
            OpenConnection();
            sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "SPForgotPassword";
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandType =
            CommandType.StoredProcedure;
            sqlCommand.Parameters.AddWithValue(
            "@Type", "0");
            sqlCommand.Parameters.AddWithValue(
            "@Email", email);
            sqlCommand.Parameters.AddWithValue(
            "@FullName", fullName);
            sqlCommand.Parameters.AddWithValue(
            "@DateOfBirth", dateOfBirth);
            forgotPasssord = Convert.ToString(
            sqlCommand.ExecuteScalar())  ;
            sqlDataReader.Close();
            sqlDataReader.Dispose();

            CloseConnection();
        }
        catch (Exception ex)
        {
            errorMessage = ex.Message;
        }
        return forgotPasssord;
    }
	
}
